using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Client;
using FreeFlow.Client.Events;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// WinForms control to diplay a list of alerts.
	/// </summary>
	public class AlertGrid : Grid
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

    /// <summary>
    /// Creates a new <see cref="AlertsList"/> instance.
    /// </summary>
		public AlertGrid()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
      CreateColumnStyles();
      dataGrid.DoubleClick += new EventHandler(dataGrid_DoubleClick);
    }

    private void CreateColumnStyles()
    {
      // create styles
      DataGridTableStyle DcStyle = new DataGridTableStyle();
      DcStyle.MappingName = AlertsList.ListDataTableName;

      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.ProcessColumn, "Process"));
      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.StageColumn, "Stage"));
      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.FolderNameColumn, "Folder name"));
      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.AlertMessageColumn, "Message"));
      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.SubjectColumn, "Subject"));
      
      // TODO - make numeric
      DataGridTextBoxColumn col = CreateColumn(AlertsList.PriorityColumn, "Priority");
      col.Alignment = HorizontalAlignment.Right;
      col.Width = 40;
      DcStyle.GridColumnStyles.Add(col);
      
      // TODO - convert to date-time columns
      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.UpdatedColumn, "Updated"));
      DcStyle.GridColumnStyles.Add(CreateColumn(AlertsList.DeadlineColumn, "Deadline"));
      
      dataGrid.TableStyles.Add(DcStyle);
    }

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      // 
      // AlertGrid
      // 
      this.Name = "AlertGrid";
      this.Size = new System.Drawing.Size(656, 344);

    }
		#endregion

    private AlertType alertListType;
    /// <summary>
    /// Gets or sets the alert list type for this grid.
    /// </summary>
    [Description("The type of alert the list will display")]
    [Category(Constants.FreeFlowCategory)]
    public AlertType AlertListType
    {
      get { return alertListType; }
      set { alertListType = value; }
    }

    /// <summary>
    /// Populates the grid.
    /// </summary>
    public void PopulateGrid()
    {
      AlertsList alertsList = new AlertsList();
      alertsList.Connection = Connection;
      alertsList.AlertListType = AlertListType;
      alertsList.RefreshItems();
      dataGrid.DataSource = alertsList.ListDataTable;
    }

    /// <summary>
    /// Event handler to indicate a folder should be opened.
    /// </summary>
    [Category(Constants.FreeFlowCategory)]
    public event EventHandler<OpenFolderEventArgs> OpenFolder;

    private void dataGrid_DoubleClick(object sender, System.EventArgs e)
    {
      if (OpenFolder != null)
      {
        BindingManagerBase bm = dataGrid.BindingContext[dataGrid.DataSource];
        DataRowView row = (DataRowView) bm.Current;
        string folderId = row["eFolderID"].ToString();
        OpenFolder(this, new OpenFolderEventArgs(folderId));
      }
    }
	}
}
